/*              Copyright(c) 2008 Dave Sexton              *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace DaveSexton.Sandcastle
{
	public abstract class UrlMappingProvider : MarshalByRefObject
	{
		#region Public Properties
		#endregion

		#region Private / Protected
		private static readonly Dictionary<Type, UrlMappingProvider> providers = new Dictionary<Type, UrlMappingProvider>();
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="UrlMappingProvider" /> class for derived classes.
		/// </summary>
		protected UrlMappingProvider()
		{
		}
		#endregion

		#region Methods
		[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]
		public static UrlMappingProvider GetProvider(string typeName, string assemblyPath)
		{
			if (string.IsNullOrEmpty(typeName))
				throw new ArgumentException(Resources.Errors.NullOrEmpty, "typeName");

			System.Reflection.Assembly assembly = (string.IsNullOrEmpty(assemblyPath))
				? System.Reflection.Assembly.GetExecutingAssembly()
				: System.Reflection.Assembly.LoadFrom(assemblyPath);

			Type type = assembly.GetType(typeName, true, false);

			return GetProvider(type);
		}

		[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]
		public static UrlMappingProvider GetProvider(Type type)
		{
			if (type == null)
				throw new ArgumentNullException("type");

			if (!providers.ContainsKey(type))
			{
				UrlMappingProvider provider = CreateProvider(type);
				providers.Add(type, provider);

				return provider;
			}
			else
				return providers[type];
		}

		[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.LinkDemand,
			Flags = System.Security.Permissions.SecurityPermissionFlag.Infrastructure)]
		public override object InitializeLifetimeService()
		{
			return null;
		}

		private static UrlMappingProvider CreateProvider(Type type)
		{
			return (UrlMappingProvider) Activator.CreateInstance(type, true);
		}

		public abstract IEnumerable<UrlMapping> GetMappings(UrlMappingGroup group, string configuration);

		public abstract System.Windows.Forms.Control GetEditor(UrlMappingGroup group, string configuration);

		public abstract string GetEditorConfiguration(UrlMappingGroup group, System.Windows.Forms.Control control);
		#endregion
	}
}
